/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controladores;

import controladores.exceptions.IllegalOrphanException;
import controladores.exceptions.NonexistentEntityException;
import controladores.exceptions.PreexistingEntityException;
import entidades.Reservarecurso;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entidades.Reserva;
import entidades.Recursos;
import java.util.ArrayList;

/**
 *
 * @author Andres
 */
public class ReservarecursoJpaController {

    public ReservarecursoJpaController() {
        emf = Persistence.createEntityManagerFactory("proyectoCongresoPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Reservarecurso reservarecurso) throws IllegalOrphanException, PreexistingEntityException, Exception {
        List<String> illegalOrphanMessages = null;
        Reserva reservaOrphanCheck = reservarecurso.getReserva();
        if (reservaOrphanCheck != null) {
            Reservarecurso oldReservarecursoOfReserva = reservaOrphanCheck.getReservarecurso();
            if (oldReservarecursoOfReserva != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The Reserva " + reservaOrphanCheck + " already has an item of type Reservarecurso whose reserva column cannot be null. Please make another selection for the reserva field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reserva reserva = reservarecurso.getReserva();
            if (reserva != null) {
                reserva = em.getReference(reserva.getClass(), reserva.getCodigoreserva());
                reservarecurso.setReserva(reserva);
            }
            Recursos recursos = reservarecurso.getRecursos();
            if (recursos != null) {
                recursos = em.getReference(recursos.getClass(), recursos.getIdrecurso());
                reservarecurso.setRecursos(recursos);
            }
            em.persist(reservarecurso);
            if (reserva != null) {
                reserva.setReservarecurso(reservarecurso);
                reserva = em.merge(reserva);
            }
            if (recursos != null) {
                recursos.getReservarecursoList().add(reservarecurso);
                recursos = em.merge(recursos);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findReservarecurso(reservarecurso.getReservaCodigoreserva()) != null) {
                throw new PreexistingEntityException("Reservarecurso " + reservarecurso + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Reservarecurso reservarecurso) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reservarecurso persistentReservarecurso = em.find(Reservarecurso.class, reservarecurso.getReservaCodigoreserva());
            Reserva reservaOld = persistentReservarecurso.getReserva();
            Reserva reservaNew = reservarecurso.getReserva();
            Recursos recursosOld = persistentReservarecurso.getRecursos();
            Recursos recursosNew = reservarecurso.getRecursos();
            List<String> illegalOrphanMessages = null;
            if (reservaNew != null && !reservaNew.equals(reservaOld)) {
                Reservarecurso oldReservarecursoOfReserva = reservaNew.getReservarecurso();
                if (oldReservarecursoOfReserva != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The Reserva " + reservaNew + " already has an item of type Reservarecurso whose reserva column cannot be null. Please make another selection for the reserva field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (reservaNew != null) {
                reservaNew = em.getReference(reservaNew.getClass(), reservaNew.getCodigoreserva());
                reservarecurso.setReserva(reservaNew);
            }
            if (recursosNew != null) {
                recursosNew = em.getReference(recursosNew.getClass(), recursosNew.getIdrecurso());
                reservarecurso.setRecursos(recursosNew);
            }
            reservarecurso = em.merge(reservarecurso);
            if (reservaOld != null && !reservaOld.equals(reservaNew)) {
                reservaOld.setReservarecurso(null);
                reservaOld = em.merge(reservaOld);
            }
            if (reservaNew != null && !reservaNew.equals(reservaOld)) {
                reservaNew.setReservarecurso(reservarecurso);
                reservaNew = em.merge(reservaNew);
            }
            if (recursosOld != null && !recursosOld.equals(recursosNew)) {
                recursosOld.getReservarecursoList().remove(reservarecurso);
                recursosOld = em.merge(recursosOld);
            }
            if (recursosNew != null && !recursosNew.equals(recursosOld)) {
                recursosNew.getReservarecursoList().add(reservarecurso);
                recursosNew = em.merge(recursosNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = reservarecurso.getReservaCodigoreserva();
                if (findReservarecurso(id) == null) {
                    throw new NonexistentEntityException("The reservarecurso with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reservarecurso reservarecurso;
            try {
                reservarecurso = em.getReference(Reservarecurso.class, id);
                reservarecurso.getReservaCodigoreserva();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The reservarecurso with id " + id + " no longer exists.", enfe);
            }
            Reserva reserva = reservarecurso.getReserva();
            if (reserva != null) {
                reserva.setReservarecurso(null);
                reserva = em.merge(reserva);
            }
            Recursos recursos = reservarecurso.getRecursos();
            if (recursos != null) {
                recursos.getReservarecursoList().remove(reservarecurso);
                recursos = em.merge(recursos);
            }
            em.remove(reservarecurso);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Reservarecurso> findReservarecursoEntities() {
        return findReservarecursoEntities(true, -1, -1);
    }

    public List<Reservarecurso> findReservarecursoEntities(int maxResults, int firstResult) {
        return findReservarecursoEntities(false, maxResults, firstResult);
    }

    private List<Reservarecurso> findReservarecursoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Reservarecurso.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Reservarecurso findReservarecurso(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Reservarecurso.class, id);
        } finally {
            em.close();
        }
    }

    public int getReservarecursoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Reservarecurso> rt = cq.from(Reservarecurso.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
